package com.bcn.login_mybatis_demo.exception;

import com.bcn.login_mybatis_demo.util.Result;
import com.bcn.login_mybatis_demo.util.ResultUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
    // 处理 HttpMessageNotReadableException 异常（请求体为空或格式错误）
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<Result> handleMessageNotReadable(HttpMessageNotReadableException e) {
        Result errorResponse = ResultUtil.error("请求体为空或格式错误，请检查请求");
        return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
    }

    // 可以根据需要添加其他异常处理方法
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Result> handleGeneralException(Exception e) {
        Result errorResponse = ResultUtil.error("服务器发生错误，请稍后再试");
        return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
